#import pytesseract
#from PIL import Image
#pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
#
#
#im=Image.open(r'C:\Users\01383317\doxx\imgg\1.jpg')
#print(pytesseract.image_to_string(im))
#
#from PIL import Image
#import pytesseract
#pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'
#
#
#im=Image.open(r'C:\Users\01383317\doxx\imgg\1.jpg')
##二值化图像传入图像和阈值
#def erzhihua(image,threshold):
#    ''':type image:Image.Image'''
#    image=image.convert('L')
#    table=[]
#    for i in range(256):
#        if i <  threshold:
#            table.append(0)
#        else:
#            table.append(1)
#    return image.point(table,'1')
#
#
#image=erzhihua(im,127)
#image.show()
#
#result=pytesseract.image_to_string(image,lang='eng')
#print(result)


from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = '15513763'
API_KEY = 'Guex1tfH7m24wZFAFRCH3yj7'
SECRET_KEY = 'FQ79GN1RXeYqaFI6Bslb75BmjL1xzDNB'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

luj = r'C:\Users\01383317\doxx\imgg\1.jpg'

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content(luj)

""" 调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image)

""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image, options)

url = "http//www.x.com/sample.jpg"

""" 调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url);

""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url, options)

(a['words_result'][0])['words']

url = 'https://cas.sf-express.com/cas//imgcode?a=0.2447101670350098'
client.basicAccurateUrl(url)

""" 调用通用文字识别（高精度版） """
client.basicAccurate(image);


a.replace(' ','')#去除空格


//*[@id="imgcode"]
yzm = brower.find_element_by_id("imgcode")


https://cas.sf-express.com/cas//imgcode


import os

# 输入目录路径，输出最新文件完整路径
def find_new_file(dir):
    '''查找目录下最新的文件'''
    file_lists = os.listdir(dir)
    file_lists.sort(key=lambda fn: os.path.getmtime(dir + "\\" + fn)
                    if not os.path.isdir(dir + "\\" + fn) else 0)
    print('最新的文件为： ' + file_lists[-1])
    file = os.path.join(dir, file_lists[-1])
    print('完整路径：', file)
    return file

# 函数调用
dir = r'D:\user\Downloads'
find_new_file(dir)


from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import re
import time
import sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from win32com import client as wc
import docx
from functools import reduce

import win32api
import win32con



brower = webdriver.Chrome(executable_path = r"D:/user/Downloads/chromedriver")
brower.get("http://itsm-rpt.sf-express.com/balantflow/")
#账号
zh = brower.find_element_by_id("username")
zh.send_keys("01383317")    
#密码RL003694795
mm = brower.find_element_by_id("password")
mm.send_keys("liuj1a!wei")

yzm = brower.find_element_by_id("imgcode")
action = ActionChains(brower).move_to_element(yzm)#移动到该元素
action.context_click(yzm)#右键点击该元素
time.sleep(1)
action.send_keys(Keys.ARROW_DOWN)#点击键盘向下箭头
time.sleep(1)
brower.send_keys('v')#键盘输入V保存图
time.sleep(1)
action.perform()#执行保存






from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import win32api
import win32con
VK_CODE ={'enter':0x0D, 'down_arrow':0x28}
#键盘键按下
def keyDown(keyName):
    win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
#键盘键抬起
def keyUp(keyName):
    win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0)
 
print('Please wait...Firefox loading...')
print('---------------------------------')

# 用浏览器实现访问
brower = webdriver.Chrome(executable_path = r"D:/user/Downloads/chromedriver")
brower.get("http://itsm-rpt.sf-express.com/balantflow/")

time.sleep(1)
yzm = brower.find_element_by_id("imgcode")
action = ActionChains(brower).move_to_element(yzm)
action.context_click(yzm).perform()
time.sleep(1)
#按v
win32api.keybd_event(86, 0, 0, 0)
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
#按enter
keyDown("enter")
keyUp("enter")

#图片的路径分片,返回完整的路径'D:\\user\\Downloads\\imgcode (7).jpg'
def codesplit(file1):
    a = file1.split('\\')[-1]
	pdress = dir1 + '\\' + a
	return pdress
    


yzm = yzmlist.pop()

if len(yzm) < 4:
    brower.refulse
    senduserpass()
    dodir(dir1)
    
if len(yzm) == 4:
    sendverifycode()
    brower.find_element_by_css_selector(".f18.logn_lt.logn_btn.lh46").click()


brower = webdriver.Chrome(executable_path = r"D:/user/Downloads/chromedriver")

def ww():
    brower.get("http://itsm-rpt.sf-express.com/balantflow/")


def downimgcode():
    yzm = brower.find_element_by_id("imgcode")
    action = ActionChains(brower).move_to_element(yzm)
    time.sleep(1)
    action.context_click(yzm).perform()
    time.sleep(1)
    #按v
    win32api.keybd_event(86, 0, 0, 0)
    win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(1)
    #按enter
    keyDown("enter")
    keyUp("enter")
	
def sendverifycode():
    fasyzm = brower.find_element_by_id("verifyCode")
    fasyzm.send_keys(yzm1)

def senduserpass():
    #账号
    zh = brower.find_element_by_id("username")
    zh.send_keys("01383317")    
    #密码RL003694795
    mm = brower.find_element_by_id("password")
    mm.send_keys("liuj1a!wei")
    #下载验证码图片
    yzm = brower.find_element_by_id("imgcode")
    action = ActionChains(brower).move_to_element(yzm)
    time.sleep(1)
    action.context_click(yzm).perform()
    time.sleep(1)
    #按v
    win32api.keybd_event(86, 0, 0, 0)
    win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(1)
    #按enter
    keyDown("enter")
    keyUp("enter")

def dodir(dir1):
    #读取下载目录的文件夹,识别最新的文件
    find_new_file(dir1)    	
    newfile1 = newfile.pop()    
    #验证码存放的路径,还需要拼接
    luj = dir1 + '\\' + newfile1
    image = get_file_content(luj)
    #调用通用文字识别
    shibyzm = client.basicAccurate(image)
	#读取字典里面的内容,最后把空格去掉
    yzm1 = ((shibyzm['words_result'][0])['words']).replace(' ', '')
    return yzmlist.append(yzm1)

docname = ['ELOG-DVP-CORE-V9.2-新特性列表.doc']
version = ['V9.2']

wjm2 = docname.pop()
banbh = version.pop()
name2 = 'ELOG-DVP-CORE-V9.2-新特性列表.docx'
name3 = r"D:\user\Downloads" + "\\" + name2


dress1 = r"D:\user\Downloads"
dress2 = r"D:\user\Downloads" + "\\" + wjm2
word = wc.Dispatch("Word.Application")
doc = word.Documents.Open(dress2)#打开文件
doc.SaveAs(name3, 12)#保存为docx
doc.Close()
word.Quit()


dress2 = r'D:\user\Downloads\SCS-OMS-CORE+V5.1.SP1+新特性列表 (1).doc'
name3 = r'D:\user\Downloads\SCS-OMS-CORE+V5.1.SP1+新特性列表 (1).docx'












